/*
  Copyright 2009 Colin Prinn

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package net.recursv.motific.unittest.view;

import net.recursv.motific.unittest.TestMidlet;
import net.recursv.motific.utils.StringList;

import javax.microedition.lcdui.*;
import java.util.Vector;

/**
 * Created by: recursv
 * Date: 3/08/2009
 * Time: 7:52:22 AM
 */
class DetailsCanvas extends Canvas implements CommandListener {
    private static final int PURPLE = 0x330099;
    private static final String Motific = "Motific @ recursv.net";

    private TestMidlet _midlet;
    private int _index = 0;
    private static final int TOP_LEFT = Graphics.TOP | Graphics.LEFT;
    private Font _font = Font.getDefaultFont();
    private boolean _scrollDownEnabled = true;

    public DetailsCanvas(TestMidlet midlet) {
        _midlet = midlet;
        addCommand(new Command("Exit", Command.EXIT, 0));
        addCommand(new Command("Running Test", Command.ITEM, 0));
        setCommandListener(this);
    }

    protected void paint(Graphics g) {
        Vector tests = _midlet.getFailedTestCases();

        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, getWidth(), getHeight());

        int detailsHeight = getHeight() - 10 - Font.getDefaultFont().getHeight();


        int yOffset = 0;
        for (int testIndex = Math.max(0, Math.min(_index, tests.size() - 1)); testIndex < tests.size() && yOffset < detailsHeight; testIndex++) {
            StringList strings = new StringList((String) tests.elementAt(testIndex), _font, getWidth() - 10);
            if (testIndex > 0) {
                g.setColor(0x666666);
                g.drawLine(5, yOffset, getWidth() - 5, yOffset);
            }
            for (int lineIndex = 0; lineIndex < strings.size() && yOffset < detailsHeight; lineIndex++, yOffset += _font.getHeight()) {
                g.setColor(0x000000);
                g.drawString(strings.elementAt(lineIndex), 5, yOffset, TOP_LEFT);
            }
            yOffset += 5;
        }
        _scrollDownEnabled = yOffset > detailsHeight;

        g.setColor(PURPLE);
        g.drawString(Motific, getWidth() - Font.getDefaultFont().stringWidth(Motific) - 10, detailsHeight + 10, TOP_LEFT);
    }

    protected void keyPressed(int keycode) {
        switch (getGameAction(keycode)) {
            case DOWN:
                if (_scrollDownEnabled) {
                    _index++;
                    repaint();
                }
                break;
            case UP:
                _index = Math.max(0, _index - 1);
                repaint();
                break;
        }
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command.getCommandType() == Command.EXIT) {
            _midlet.notifyDestroyed();
        } else if (command.getCommandType() == Command.ITEM) {
            _midlet.setCanvas(new TestsCanvas(_midlet));
        }
    }
}
